package cn.slt.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.slt.service.MD_libraryDSIDService;
import cn.slt.service.MD_testConnection;


@Controller

public class MD_testConnectionController {
	@Autowired
	private MD_testConnection service;
	
	@RequestMapping("testConnection")
	@ResponseBody
	public String testConnection(@RequestParam String url,@RequestParam  String username,@RequestParam  String password, @RequestParam  String owner, @RequestParam  String database,HttpServletRequest req) throws UnsupportedEncodingException
	{   boolean mark=false;
	    String words=null;
	   url=url.trim();
	   username=username.trim();
	   password=password.trim();
	   owner=owner.trim();
	    
	   // String birthday = req.getParameter("birthday");
	  
	//    System.out.println(url+username+password+owner);
	   
	    mark=service.testConnection(url, username, password, owner,database);
//	    if(mark){
//			try {
//				response.getWriter().write("a");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		else{
//			try {
//				response.getWriter().print(0);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
	    System.out.println("service"+mark);
	    if(mark) words="1";
	    else words="2";
	    
		return words;
	}
	
	
	
	
	@ResponseBody
	@RequestMapping("getConnection")
	public String getConnection(@RequestParam String url,@RequestParam  String username,@RequestParam  String password, @RequestParam  String owner,@RequestParam  String database,HttpServletRequest req,	HttpServletResponse response) throws UnsupportedEncodingException
	{   boolean mark=false;
	    String words=null;
	       url=url.trim();
		   username=username.trim();
		   password=password.trim();
		   owner=owner.trim();
	    System.out.println(req.getParameterMap());
	    System.out.println(url+username+password+owner);
	    mark=service.getConnection(url, username, password, owner,database);
//	    if(mark){
//			try {
//				response.getWriter().write("a");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		else{
//			try {
//				response.getWriter().print(0);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
	    System.out.println("service"+mark);
	    if(mark) words="1";
	    else words="2";
	    
		return words;
	}
	
	
	

}
